package com.wengluobiancheng.demo06;

import java.io.*;
import java.net.Socket;
import java.util.UUID;

public class ThreadSocket01 implements Runnable {
    private Socket acceptSocket;

    public ThreadSocket01(Socket accept) {
        this.acceptSocket = accept;
    }

    @Override
    public void run() {
        BufferedOutputStream bos = null;
        try {
            BufferedInputStream bis = new BufferedInputStream(acceptSocket.getInputStream());
            bos = new BufferedOutputStream(new FileOutputStream("D:\\itheima\\" + UUID.randomUUID().toString() + ".jpg"));
            int v;
            while ((v = bis.read()) != -1) {
                bos.write(v);
            }
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(acceptSocket.getOutputStream()));
            bw.write("上传成功");
            bw.flush();
            bw.newLine();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (acceptSocket != null) {
                try {
                    acceptSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
